package br.edu.ufc.deti.servlets;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;

import br.edu.ufc.deti.beans.Autenticacao;
import br.edu.ufc.deti.beans.PerfClusters;
import br.edu.ufc.deti.beans.Recurso;

import br.edu.ufc.deti.broker.mapeamento.ExecucaoMapping;
import br.edu.ufc.deti.broker.mapeamento.bean.PerfClustersDecorator;
import br.edu.ufc.deti.broker.services.AutenticacaoProxy;
import br.edu.ufc.deti.cripto.Crypto;

@WebServlet("/ServletAutenticacao")
public class ServletAutenticacao extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		HttpSession session = req.getSession(); 
		PerfClustersDecorator pcDecorator = new PerfClustersDecorator();
		pcDecorator.setPerfClusters(request2PerfClusters(req));
		AutenticacaoProxy autenticacaoProxy = new AutenticacaoProxy();
		String proximo = "";
		try{
			String perfClustersXML = ExecucaoMapping.getInstance().marshalPerfClusters(pcDecorator);
			String XMLResultado = autenticacaoProxy.autenticaUsuario(perfClustersXML);
			PerfClusters perfClusters  = ExecucaoMapping.getInstance().unmarshalPerfClusters(XMLResultado, pcDecorator);
			if ( perfClusters.getAutenticacao().getID() > 0 ){
				List<Recurso> recursos = perfClusters.getRecursos();
				String recursos_usuario = "";
				for ( Recurso recurso : recursos ) {
					String hostRecurso = recurso.getHostRecurso();
					String ipRecurso = recurso.getIpRecurso();
					recursos_usuario += 
					"<tr>"+
				    	"<td align='center'>"+
				    		"<input checked='checked' name='"+hostRecurso+"' id='"+hostRecurso+"' value='"+ipRecurso+"' type='checkbox'>"+
				    	"</td>"+
				    	"<td>"+hostRecurso+"</td>"+
				    "</tr>";
				}
				session.setAttribute("perfClusters", perfClusters);
				session.setAttribute("recursos_tela", recursos_usuario);
				session.setAttribute("usuario", perfClusters.getAutenticacao().usuario);
				session.setAttribute("cota", perfClusters.getAutenticacao().usuario);
				proximo = "/jsps/requisitos_usuarios.jsp";
			}else{
				session.setAttribute("autenticacao", "nao_autenticado");
				proximo = "/index.jsp";
			}
			RequestDispatcher rd = getServletContext().getRequestDispatcher( proximo );
			rd.forward(req, res);
		} catch (MarshalException e) {
			e.printStackTrace();
		} catch (ValidationException e) {
			e.printStackTrace();
		} catch (MappingException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @param req
	 * @return
	 */
	private PerfClusters request2PerfClusters( HttpServletRequest req ) {
		PerfClusters perfclusters = new PerfClusters();
		Autenticacao autenticacao = new Autenticacao();
		autenticacao.setUsuario(req.getParameter("usuario"));
		try {
			//autenticacao.setSenha(Crypto.encrypt(req.getParameter("senha")));
			autenticacao.setSenha(req.getParameter("senha"));
/*		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
*/		} catch (IllegalStateException e) {
			e.printStackTrace();
/*		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
*/		}
		perfclusters.setAutenticacao(autenticacao);
		return perfclusters;
	}
}
